from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import ForeignKey, String, Text
from datetime import datetime

db = SQLAlchemy()

# define "users" model
class User(db.Model):
    __tablename__ = "users" # define table name
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    username: Mapped[str] = mapped_column(String(80), unique=True, nullable=False)
    password: Mapped[str] = mapped_column(Text, nullable=False)

    def __repr__(self):
        return f"<User {self.username}>"

# define "posts" model
class Post(db.Model):
    __tablename__ = "posts" # define table name
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    author_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
    created: Mapped[datetime] = mapped_column(default=datetime.now, nullable=False)
    title: Mapped[str] = mapped_column(String(100), nullable=False)
    body: Mapped[str] = mapped_column(Text, nullable=False)

    def __repr__(self):
        return f"<Post {self.title}>"